//
//  ViewWebViewController.m
//  PrinterDemo
//
//  Created by 唐欢 on 2017/9/26.
//

#import "ViewWebViewController.h"
#import "PrinterSDK.h"
#import "UIWebView+UIImage.h"
#import "HLPrinter.h"

@interface ViewWebViewController ()

@property (nonatomic, strong) UIWebView * webView;

@end

@implementation ViewWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    UIWebView * webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:webView];
    self.webView = webView;
    
    NSURL * filePath =  [[NSBundle mainBundle] URLForResource:@"index" withExtension:@"html" subdirectory:nil];
    NSURLRequest *request = [NSURLRequest requestWithURL:filePath];

    [webView loadRequest:request];
    
    
    UIButton * btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    [btn setTitle:@"取消" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    
    
    
    UIButton * btn1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    [btn1 setTitle:@"打印" forState:UIControlStateNormal];
    [btn1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [btn1 addTarget:self action:@selector(print) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn1];
}

- (void)print{
    
    UIImage * image = [self.webView imageForWebView];
    
    image = [self OriginImage:image scaleToSize:CGSizeMake(300, image.size.height/image.size.width*300)];
    
    [[PrinterSDK defaultPrinterSDK] printImage:image];
    
    
    HLPrinter *printer = [[HLPrinter alloc] init];

    [printer appendImage:image alignment:HLTextAlignmentLeft maxWidth:450];

    
//    UIImageView * v = [[UIImageView alloc] initWithImage:image];
//    [self.view addSubview:v];
}
- (void)btnClick{
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(UIImage*) OriginImage:(UIImage *)image scaleToSize:(CGSize)size
{
    UIGraphicsBeginImageContext(size);  //size 为CGSize类型，即你所需要的图片尺寸
    
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return scaledImage;   //返回的就是已经改变的图片
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
